package com.jhh.pay.center.service.impl;

import com.jhh.pay.center.cfg.caching.Cache;
import com.jhh.pay.center.dao.PayChannelMapper;
import com.jhh.pay.center.model.PayChannel;
import com.jhh.pay.center.service.PayChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author tangxd
 * @Description: TODO
 * @date 2017/12/5
 */
@Service
@Slf4j
public class PayChannelServiceImpl implements PayChannelService {

    @Autowired
    private PayChannelMapper channelMapper;

    @Autowired
    private Cache cache;

    private String cacheKey(int id) {
        return String.format("jhh:pay:channel:%d", id);
    }

    private String cacheKeyAll() {
        return "jhh:pay:channel:all";
    }

    @Override
    public boolean save(PayChannel channel) {
        channel.setKey(channel.getKey().toLowerCase());
        if (getByKey(channel.getKey()) != null) {
            return false;
        }
        boolean ok = channelMapper.insert(channel) > 0;
        if (ok) {
            cache.set(cacheKey(channel.getId()), channel, TimeUnit.SECONDS.convert(3, TimeUnit.DAYS));
            cache.del(cacheKeyAll());
        }
        return ok;
    }

    @Override
    public boolean update(PayChannel channel) {
        boolean ok = channelMapper.update(channel) > 0;
        if (ok) {
            cache.set(cacheKey(channel.getId()), channel, TimeUnit.SECONDS.convert(3, TimeUnit.DAYS));
        }
        return ok;
    }

    @Override
    public PayChannel get(int id) {
        if (id <= 0) {
            return null;
        }
        PayChannel pc = cache.get(cacheKey(id), PayChannel.class);
        if (null == pc) {
            pc = channelMapper.get(id);
            cache.set(cacheKey(id), pc, TimeUnit.SECONDS.convert(3, TimeUnit.DAYS));
        }
        return pc;
    }

    @Override
    public Collection<PayChannel> gets() {
        List<Integer> ids = cache.getList(cacheKeyAll(), Integer.class);
        if (ids != null && ids.size() > 0) {
            return idsToChannels(ids);
        }
        Collection<Integer> newIds = channelMapper.gets();
        cache.set(cacheKeyAll(), newIds, TimeUnit.SECONDS.convert(5, TimeUnit.DAYS));
        return idsToChannels(newIds);
    }

    private Collection<PayChannel> idsToChannels(Collection<Integer> ids) {
        if (ids != null && ids.size() > 0) {
            List<PayChannel> list = new ArrayList<>();
            for (Integer id : ids) {
                list.add(get(id));
            }
            return list;
        }
        return new ArrayList<>();
    }

    @Override
    public PayChannel getByKey(String key) {
        Collection<PayChannel> channels = gets();
        for (PayChannel channel : channels) {
            if (channel.getKey().equals(key.toLowerCase())) {
                return channel;
            }
        }
        return null;
    }
}
